using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace Konst3d.Efficiency
{
    public class NotifiableArray<T>
    {
        #region Types

        public delegate void ArrayItemChanged<T>(int index, T oldValue, T newValue);

        #endregion // Types

        #region Properties

        public int Length
        {
            get
            {
                return _data.Length;
            }
        }

        public T this[int index]
        {
            get
            {
                Contract.Assert(index < _data.Length);

                return _data[index];
            }

            set
            {
                Contract.Assert(index < _data.Length);

                T old = _data[index];

                _data[index] = value;

                if (ItemChanged != null)
                {
                    ItemChanged(index, old, value);
                }
            }
        }

        #endregion // Properties

        #region Events

        public event ArrayItemChanged<T> ItemChanged;

        #endregion // Events

        #region Constructor

        public NotifiableArray(uint length)
        {
            _data = new T[length];
        }

        #endregion // Constructor

        #region Fields

        private T[] _data;

        #endregion // Fields
    }
}
